package com.app.controller;

import com.alibaba.fastjson.JSONObject;
import com.app.common.Result;
import com.app.model.KeyWorkModel;
import com.app.model.ProjectModel;
import com.app.service.KeyWorkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * 重点工作任务完成情况
 */
@Controller
@RestController
@RequestMapping("/api/work")
public class KeyWorkController {
    @Autowired
    private KeyWorkService keyWorkService;

    //查询全部
    @GetMapping("/all")
    public Result<Object> getAll(Integer type) {
        return Result.success(keyWorkService.keyWorkList(type));
    }
    //查看内容
    @GetMapping("/content")
    public Result<Object> getContentById(Integer id) {
        return Result.success(keyWorkService.contentList(id));
    }


    //重点工作后台
    //添加 //编辑
    @PostMapping("/insertAndUpdate")
    public Result<Object> insertAndUpdate(@RequestBody KeyWorkModel model) {
        if (model.getId() == 0) {
            model.setCreateTime(LocalDateTime.now());
            keyWorkService.insert(model);

        } else {
            model.setUpdateTime(LocalDateTime.now());
            keyWorkService.set(model);
        }
        return Result.success("操作成功");
    }

    //删除
    @GetMapping("/delete")
    public Result<Object> deleteProjectById(int id) {
        keyWorkService.deleteProjectById(id);
        return Result.success("操作成功！");
    }

    //查询列表
    //根据关键字查询
    @GetMapping("/getAll")
    public Result<Object> getAll(int page, int limit, String task, String year) {
        KeyWorkModel model = new KeyWorkModel();
        model.setPage((page - 1) * limit);
        model.setLimit(limit);
        model.setTask(task);
        model.setYear(year);
        return Result.success(keyWorkService.keyWork(model));

    }

}
